package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.NumberUtils;
import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.domian.BaseInfo;
import org.jsbd.boss.domian.statistics.UserClientInfoUseRatio;
import org.jsbd.boss.domian.statistics.UserClientinfoDay;
import org.jsbd.boss.domian.statistics.UserIncrVO;
import org.jsbd.boss.enums.CarrierEnum;
import org.jsbd.boss.service.IBaseInfoService;
import org.jsbd.boss.service.IMiguRegisterStatisticsService;
import org.jsbd.boss.service.IUserIncrService;
import org.jsbd.boss.util.BigDecimalUtil;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.NumUtil;
import org.jsbd.boss.util.ProvinceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user/incr")
public class UserIncrController {

	private static final Logger logger = LoggerFactory.getLogger(UserIncrController.class);

	@Resource
	private IUserIncrService userIncrService;
	@Resource
	private IBaseInfoService baseInfoService;
	@Resource
	private IMiguRegisterStatisticsService miguRegisterStatisticsService;

	/**
	 * 现网新增用户页面
	 * 
	 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list() {
		return "userIncr/userIncrList";
	}

	/**
	 * 现网新增用户数据
	 * 
	 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
	 * @param page
	 *            分页信息
	 * @param userIncrVO
	 *            查询条件
	 * @return
	 */
	@RequestMapping(value = "/listJson", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listJson(DataGridModel page, UserIncrVO userIncrVO) {
		return userIncrService.getIncrUserWithPage(page, userIncrVO);
	}

	/**
	 * 现网新增用户比较分析页面
	 * 
	 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
	 * @return
	 */
	@RequestMapping(value = "/userIncrAnalyze")
	public String analyzeDataPage() {
		return "userIncr/userIncrAnalyze";
	}

	/**
	 * 现网新增用户比较分析
	 * 
	 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
	 * @param model
	 * @param startTime
	 * @return
	 */
	@RequestMapping(value = "analyzeData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> analyzeData(Model model, String startTime) {
		Map<String, Object> map = new HashMap<String, Object>();

		if (StringUtils.isBlank(startTime)) {
			startTime = DateUtil.getCurrentDateFormatStr(DateUtil.DATE_PATTEN_YYMMDD);
		}
		if (!StringUtils.isBlank(startTime)) {
			startTime = startTime.replace("-", "");
		}

		List<String> xAxis = new ArrayList<String>();
		List<String> provinces = userIncrService.getAnalyzeDataxAxis(startTime);
		for (String province : provinces) {
			xAxis.add(ProvinceUtils.getProvinceName(province));
		}
		List<Integer> dianxin = userIncrService.getAnalyzeDatayAxis(startTime, CarrierEnum.valueOf(0).getType());
		List<Integer> liantong = userIncrService.getAnalyzeDatayAxis(startTime, CarrierEnum.valueOf(1).getType());
		List<Integer> yidong = userIncrService.getAnalyzeDatayAxis(startTime, CarrierEnum.valueOf(2).getType());

		List<UserClientinfoDay> pies = userIncrService.getAnalyzeDataPie(startTime);

		map.put("xAxis", xAxis);
		map.put("yidongNum", yidong);
		map.put("dianxinNum", dianxin);
		map.put("liantongNum", liantong);
		map.put("pies", pies);

		return map;
	}

	/**
	 * 新增用户SDK利用率页面
	 * 
	 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
	 * @return
	 */
	@RequestMapping(value = "/sdkUseRatePage")
	public String sdkUseRatePage() {
		return "userIncr/userIncrUseRate";
	}

	/**
	 * 新增用户SDK利用率
	 * 
	 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
	 * @param model
	 * @param startTime
	 * @return
	 */
	@RequestMapping(value = "sdkUseRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> sdkUseRateData(Model model, String startTime) {
		Map<String, Object> map = new HashMap<String, Object>();

		if (StringUtils.isBlank(startTime)) {
			startTime = DateUtil.getCurrentDateFormatStr(DateUtil.DATE_PATTEN_YY_MM_DD);
		}

		List<String> xAxis = new ArrayList<String>();
		List<String> bases = userIncrService.getUseRateDataxAxis(startTime);
		List<BaseInfo> baseInfos = baseInfoService.getAllChannelInfo();
		Map<String, String> biMap = new HashMap<String, String>();
		for (BaseInfo baseInfo : baseInfos) {
			biMap.put(baseInfo.getId(), baseInfo.getBaseName());
		}
		for (String base : bases) {
			if ("-1".equals(base)) {
				xAxis.add("总量");
			} else {
				xAxis.add(biMap.get(base));
			}
		}

		List<UserClientInfoUseRatio> yAxis = userIncrService.getUseRateDatayAxis(startTime);
		// 咪咕注册成功用户数
		String rd = startTime.replaceAll("-", "");
		Integer miguNum = miguRegisterStatisticsService.getMiguNumByRd(rd);
		if (null == miguNum) {
			miguNum = 0;
		}

		for (UserClientInfoUseRatio uciur : yAxis) {
			Long newNum = uciur.getNewNum();
			Long useNum = uciur.getUseNum();

			uciur.setMiguNum(miguNum);
			uciur.setPureRate(BigDecimalUtil.divideToLongValueDouble(useNum, newNum - miguNum));
		}

		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);

		return map;
	}

	/**
	 * 新增用户SDK利用率环比页面
	 * 
	 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
	 * @return
	 */
	@RequestMapping(value = "/sdkUseRateQoqPage")
	public String sdkUseRateQoqPage() {
		return "userIncr/userIncrUseQoqRate";
	}

	/**
	 * 新增用户SDK利用率环比
	 * 
	 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
	 * @param model
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param base
	 *            基地
	 * @return 新增用户SDK利用率环比数据
	 */
	@RequestMapping(value = "sdkUseRateQoqData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> sdkUseRateQoqData(Model model, String startTime, String endTime, Integer base, Integer carrier) {
  		Map<String, Object> map = new HashMap<String, Object>();
  		
  		if (StringUtils.isBlank(startTime)) {
  			startTime = DateUtil.getDayBeforeDate(DateUtil.DATE_PATTEN_YY_MM_DD, 30);
  		}
  		
  		List<String> xAxis =  userIncrService.getUseRateQoqDataxAxis(startTime, endTime, base);
  		List<UserClientInfoUseRatio> yAxis = userIncrService.getUseRateQoqDatayAxis(startTime, endTime, base, carrier);
  		for (int i = 0; i < yAxis.size(); i++) {
  			UserClientInfoUseRatio uciur = yAxis.get(i);
  			Long newNum = uciur.getNewNum();
  			Long useNum = uciur.getUseNum();
  			
  			String rd = uciur.getRd().replaceAll("-", "");
  			Integer miguNum = miguRegisterStatisticsService.getMiguNumsByRd(rd);
  			
  			uciur.setMiguNum(miguNum);
  			uciur.setPureRate(BigDecimalUtil.divideToLongValueDouble(useNum, newNum-miguNum));
  		}
  		
		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);
		
		return map;
	}
	
}
